package com.tarena.lbs.coupon.utils;

import com.tarena.lbs.coupon.pojo.dto.CouponDto;
import com.tarena.lbs.coupon.pojo.po.CouponPO;

import java.sql.Date;

/**
 * 优惠券DTO和PO之间的转换工具类
 */
public class CouponConverter {

    /**
     * 将CouponDto转换为CouponPO
     *
     * @param couponDto 待转换的CouponDto对象
     * @return 转换后的CouponPO对象
     */
    public static CouponPO toCouponPO(CouponDto couponDto) {
        if (couponDto == null) {
            return null;
        }

        CouponPO couponPO = new CouponPO();

        couponPO.setId(couponDto.getId());
        couponPO.setCouponName(couponDto.getCouponName());
        couponPO.setCouponType(couponDto.getCouponType());
        couponPO.setDiscountValue(couponDto.getDiscountValue());
        couponPO.setMaxDiscountAmount(couponDto.getMaxDiscountAmount());
        couponPO.setApplicable(couponDto.getApplicable());
        couponPO.setUsageLimit(couponDto.getUsageLimit());
        couponPO.setMaxUsageLimit(couponDto.getMaxUsageLimit());

        // 日期类型转换: LocalDate -> Date
        if (couponDto.getStartDate() != null) {
            couponPO.setStartDate(Date.valueOf(couponDto.getStartDate()));
        }
        if (couponDto.getEndDate() != null) {
            couponPO.setEndDate(Date.valueOf(couponDto.getEndDate()));
        }

        couponPO.setDescribes(couponDto.getDescribes());
        couponPO.setBusinessId(couponDto.getBusinessId());
        couponPO.setExclusionType(couponDto.getExclusionType());
        couponPO.setOrderAmount(couponDto.getOrderAmount());
        couponPO.setUsageNum(couponDto.getUsageNum());

        return couponPO;
    }

    /**
     * 将CouponPO转换为CouponDto
     *
     * @param couponPO 待转换的CouponPO对象
     * @return 转换后的CouponDto对象
     */
    public static CouponDto toCouponDto(CouponPO couponPO) {
        if (couponPO == null) {
            return null;
        }

        CouponDto couponDto = new CouponDto();

        couponDto.setId(couponPO.getId());
        couponDto.setCouponName(couponPO.getCouponName());
        couponDto.setCouponType(couponPO.getCouponType());
        couponDto.setDiscountValue(couponPO.getDiscountValue());
        couponDto.setMaxDiscountAmount(couponPO.getMaxDiscountAmount());
        couponDto.setApplicable(couponPO.getApplicable());
        couponDto.setUsageLimit(couponPO.getUsageLimit());
        couponDto.setMaxUsageLimit(couponPO.getMaxUsageLimit());

//      日期类型转换: Date -> LocalDate
// 日期类型转换: Date -> LocalDate
        if (couponPO.getStartDate() != null) {
            couponDto.setStartDate(couponPO.getStartDate().toInstant()
                    .atZone(java.time.ZoneId.systemDefault())
                    .toLocalDate());
        }
        if (couponPO.getEndDate() != null) {
            couponDto.setEndDate(couponPO.getEndDate().toInstant()
                    .atZone(java.time.ZoneId.systemDefault())
                    .toLocalDate());
        }

        couponDto.setDescribes(couponPO.getDescribes());
        couponDto.setBusinessId(couponPO.getBusinessId());
        couponDto.setExclusionType(couponPO.getExclusionType());
        couponDto.setOrderAmount(couponPO.getOrderAmount());
        couponDto.setUsageNum(couponPO.getUsageNum());

        return couponDto;
    }
}